package quest.handler
{
import flash.errors.IllegalOperationError;

import quest.IQuestManager;

import utils.VectorUtil;

import values.quest.QuestGoalValue;

/**
 * QuestGoalHandlerBase
 * @author jliang
 * 
 */
public class QuestGoalHandlerBase implements IQuestGoalHandler
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function QuestGoalHandlerBase(manager:IQuestManager)
	{
		_manager = manager;
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	protected var _manager:IQuestManager;
	
	public function get manager():IQuestManager
	{
		return _manager;
	}
	
	public function get key():String
	{
		throw new IllegalOperationError("key getter should be overrided.");
	}
	
	protected var goals:Vector.<QuestGoalValue> = new Vector.<QuestGoalValue>();
	
	public function get isEmpty():Boolean
	{
		return goals.length == 0;
	}
	
	public function addTargetQuestGoal(goal:QuestGoalValue):void
	{
		goals.push(goal);
	}
	
	public function removeTargetQuestGoal(goal:QuestGoalValue):void
	{
		VectorUtil.removeObject(goal, goals);
	}
	
	public function check(type:String, ...args):void
	{
		for each (var goal:QuestGoalValue in goals)
		{
			if (!goal.owner.isAccepted || goal.isCompleted) continue;
			checkGoal.apply(this, [ goal, type ].concat(args));
		}
	}
	
	public function dispose():void
	{
		goals = null;
	}
	
	protected function checkGoal(goal:QuestGoalValue, type:String, ...args):void
	{
		throw new IllegalOperationError("checkGoal method should be overrided.");
	}
}
}


